<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
<!--      <el-button @click="to_edit(row)" size="small">详情</el-button>-->
      <el-button v-for="btn in row.btns" :type="btn.type" size="small" @click="btn.onclick(row)">{{ btn.title }}</el-button>
    </template>
  </list_url>
</template>
<script>
import {_href} from "@/js/s94_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');

    if(in_right_admin('UserMoneyRecharge/export')) btns['导出'] = function(){
      let param = s94Mjs.param_encode(app.$refs['list_url'].$refs['forms'].form_data());
      window.open(_href("/admin/user_money_recharge/export?"+param), '_blank')
    }


    let status_config = this.admin_config('status_alias_map.order_pay.status');
    var order_refund_status_config = app.admin_config('status_alias_map.order_refund.status');
    let pay_type_config = this.admin_config('status_alias_map.order_pay.pay_type',[]);

    status_config = app.decode_config(status_config);
    status_config.unshift({value:"", content:"全部"});
    order_refund_status_config = app.decode_config(order_refund_status_config);
    order_refund_status_config.unshift({value:"", content:"全部"});

    let forms_config = {
      create_time: {label:'充值时间',name:'where[create_time][between]',type:'date_between',add_radio:true,config:'Y-M-D',value:'',size:''},
      br1: {label:'',name:'',type:'br',value:''},
      order_refund_refund_time: {label:'退款时间',name:'where[order_refund.refund_time][between]',type:'date_between',add_radio:true,config:'Y-M-D',value:'',size:''},
      br11: {label:'',name:'',type:'br',value:''},
      status: {label:'订单状态',name:'where[status][eq]',type:'radio',config:status_config,value:'',button:true,size:''},
      br2: {label:'',name:'',type:'br',value:''},
      refund_status: {label:'退款状态',name:'where[order_refund.status][eq]',type:'radio',config:order_refund_status_config,value:'',button:true,size:''},
      br3: {label:'',name:'',type:'br',value:''},
      pay_money: {label:'充值金额',name:'where[pay_money][between]',type:'text_between',config:"number",value:'',size:''},
      br4: {label:'',name:'',type:'br',value:''},
      pay_type: {label:'支付方式',name:'where[pay_type][eq]',type:'radio',config:Object.assign({"":"全部"},pay_type_config),value:'',button:true,size:''},
      br5: {label:'',name:'',type:'br',value:''},
      order_no: {label:'订单编号',name:'where[order_no][like]',type:'text_one',value:''},
      user_phone: {label:'用户电话',name:'where[user.phone][eq]',type:'text_one',config:"number",value:'',no_table:true},
    };
    let list_cols = {
      id:{name:'id', label:'ID', width:'65px', fixed:'left', type: 'hidden', order:true},
      order_no: {name:'order_no', label:'订单号', type:'text_more', width:'100px'},
      pay_type:{name:'pay_type', label:'支付方式', width:'120px', type:'select_one', config: pay_type_config},
      user:{name:'user', label:'下单用户', type:'json_param', config:[{"name":"nickname","label":"昵称"},{"name":"phone","label":"电话"}], align:'left'},
      pay_money:{name:'pay_money', label:'金额', width: '120px'},
      status:{name:'status', label:'支付状态', width: '60px', type:'select_one',config:status_config},
      order_refund_status:{name:'order_refund_status', label:'退款状态', width: '60px', type:'select_one',config:order_refund_status_config},
      order_refund_refund_time:{name:'order_refund_refund_time', label:'退款时间', width:'165px'},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'充值时间', width:'165px'};
    }
    if (!this.admin_config('this_tenant.id')){
      list_cols['tenant_id'] = {name:'tenant_id', label:'租户', width:'65px', fixed:'right'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'120px', type:'action', fixed:'right'};

    function action_btns(row){
      let res = [];
      if (row.status == 2 && ~[null,0,3,6].indexOf(row.order_refund_status)){
        res.push({onclick: app.refund, title: '退款'})
      }
      return res;
    }

    return {
      config: {
        api: '/admin/user_money_recharge/'+(is_del?'recycle':'list'),
        title: "用户充值列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          s94Mjs.each(list, function (row){
            row.user = JSON.stringify({nickname:row.user_nickname, phone:row.user_phone});
            row.btns = action_btns(row);
          })
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
    }
  },
  methods: {
    to_edit(row){
      let can_edit = in_right_admin('UserMoneyRecharge/edit') && ~[0,row.tenant_id].indexOf(this.admin_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      this.iframe('edit?'+s94Mjs.param_encode(param))
    },
    refund(row){
      let app = this;
      this.$confirm('是否确定退款').then(()=>{
        ajax({url:'/admin/user_money_recharge/edit_refund?id='+row.id},(res)=>{
          app.$refs['list_url'].list();
        })
      }).catch(()=>{})
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

  }
}
</script>
